<?php
require_once 'header.php';

// 检查是否已登录
if (!isLoggedIn()) {
    $_SESSION['flash_message'] = '请先登录';
    $_SESSION['flash_message_type'] = 'warning';
    header('Location: login.php');
    exit;
}

$user_id = $_SESSION['user_id'];

// 处理GET请求 - 清空购物车
if ($_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['action']) && $_GET['action'] === 'clear') {
    global $conn;
    
    $sql = "DELETE FROM cart WHERE user_id = ?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("i", $user_id);
    $success = $stmt->execute();
    
    if ($success) {
        $_SESSION['flash_message'] = '购物车已清空';
        $_SESSION['flash_message_type'] = 'success';
    } else {
        $_SESSION['flash_message'] = '清空购物车失败，请重试';
        $_SESSION['flash_message_type'] = 'danger';
    }
    
    header('Location: cart.php');
    exit;
}

// 处理POST请求 - 更新购物车
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $cart_id = isset($_POST['cart_id']) ? (int)$_POST['cart_id'] : 0;
    $action = isset($_POST['action']) ? $_POST['action'] : '';
    
    if ($cart_id <= 0) {
        $_SESSION['flash_message'] = '无效的购物车项目';
        $_SESSION['flash_message_type'] = 'danger';
        header('Location: cart.php');
        exit;
    }
    
    // 获取当前购物车项目的信息
    global $conn;
    $sql = "SELECT quantity FROM cart WHERE id = ? AND user_id = ?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("ii", $cart_id, $user_id);
    $stmt->execute();
    $result = $stmt->get_result();
    
    if ($result->num_rows === 0) {
        $_SESSION['flash_message'] = '购物车项目不存在';
        $_SESSION['flash_message_type'] = 'danger';
        header('Location: cart.php');
        exit;
    }
    
    $item = $result->fetch_assoc();
    $current_quantity = $item['quantity'];
    
    // 处理不同的操作
    switch ($action) {
        case 'increase':
            if ($current_quantity < 10) { // 设置最大购买数量为10
                $new_quantity = $current_quantity + 1;
                updateCartQuantity($cart_id, $user_id, $new_quantity);
                $_SESSION['flash_message'] = '购物车已更新';
                $_SESSION['flash_message_type'] = 'success';
            } else {
                $_SESSION['flash_message'] = '已达到最大购买数量';
                $_SESSION['flash_message_type'] = 'warning';
            }
            break;
            
        case 'decrease':
            if ($current_quantity > 1) {
                $new_quantity = $current_quantity - 1;
                updateCartQuantity($cart_id, $user_id, $new_quantity);
                $_SESSION['flash_message'] = '购物车已更新';
                $_SESSION['flash_message_type'] = 'success';
            } else {
                // 如果数量为1，则删除该项目
                removeFromCart($cart_id, $user_id);
                $_SESSION['flash_message'] = '商品已从购物车中移除';
                $_SESSION['flash_message_type'] = 'success';
            }
            break;
            
        case 'remove':
            removeFromCart($cart_id, $user_id);
            $_SESSION['flash_message'] = '商品已从购物车中移除';
            $_SESSION['flash_message_type'] = 'success';
            break;
            
        default:
            $_SESSION['flash_message'] = '无效的操作';
            $_SESSION['flash_message_type'] = 'danger';
    }
    
    header('Location: cart.php');
    exit;
} else {
    // 如果不是POST或有效的GET请求，重定向到购物车页面
    header('Location: cart.php');
    exit;
} 